package com.oyjd.ybx.business.msg.service.impl;

import java.io.File;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.icu.text.SimpleDateFormat;
import com.oyjd.ybx.bean.po.Information;
import com.oyjd.ybx.bean.po.MsgCount;
import com.oyjd.ybx.bean.vo.InformationVO;
import com.oyjd.ybx.business.msg.dao.MsgDao;
import com.oyjd.ybx.business.msg.service.MsgService;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.FileOP;
import com.oyjd.ybx.fw.util.RandUtil;

/**
 * 
 * 资讯信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class MsgServiceImpl implements MsgService {
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
	
	private MsgDao dao;

	@Autowired
	public void setDao(MsgDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取资讯信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<InformationVO> getList(Page<InformationVO> page) {
		return dao.getList(page);
	}

	/**
	 * 根据ID获取资讯信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public InformationVO getInformationVO(String msgId) {
		return dao.getInformationVO(msgId);
	}
	
	/**
	 * 统计浏览量
	 * 
	 * @param msgId
	 *            文章ID
	 * @param ip
	 *            请求地址
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = false)
	public void countLook(String msgId, String ip){
		String key = ip + "_" + sdf.format(new Date());
		List tmp = dao.queryByHql("from " + MsgCount.class.getName() + " where msgId=? and ipTime=?", new Object[] { msgId, key });
		if (0 == tmp.size()) {
			MsgCount mc=new MsgCount();
			mc.setMsgId(msgId);
			mc.setIpTime(key);
			dao.create(mc);
			
			dao.executeHql("update " + Information.class.getName() + " g set g.countLook=(select count(t.id) from " + MsgCount.class.getName() + " t where t.msgId=g.msgId) where g.msgId=?", new Object[] { msgId });
		}
	}

	/**
	 * 增加资讯信息
	 * 
	 * @param classId
	 *            分类ID
	 * @param msgTitle
	 *            资讯标题
	 * @param msgContent
	 *            资讯内容
	 * @param fileName
	 *            图片名
	 * @param file
	 *            图片文件
	 * @param user
	 *            当前用户
	 * @throws Exception 
	 */
	@Transactional(readOnly = false)
	public void add(String classId, String msgTitle, String msgContent, String fileName, File file, String user) throws Exception {
		Information information = new Information();
		information.setClassId(classId);
		information.setMsgTitle(msgTitle);
		information.setMsgContent(msgContent);
		if (null != fileName && !"".equals(fileName)) {
			String picName = FileOP.getNewUUIDName(fileName);
			FileOP.writeFile(CodeUtil.picPath + picName, file);
			information.setMsgPic(picName);
		}		
		information.setCreator(user);
		information.setCreateTime(new Date());
		information.setOrderKey(System.currentTimeMillis());
		dao.create(information);
		getFrontInfoByDb();
	}

	/**
	 * 修改资讯信息
	 * 
	 * @param msgId
	 *            资讯ID
	 * @param classId
	 *            分类ID
	 * @param msgTitle
	 *            资讯标题
	 * @param msgContent
	 *            资讯内容
	 * @param fileName
	 *            图片名
	 * @param file
	 *            图片文件
	 * @throws Exception 
	 */
	@Transactional(readOnly = false)
	public void edit(String msgId, String classId, String msgTitle, String msgContent, String fileName, File file) throws Exception {
		Information information = (Information) dao.queryById(Information.class, msgId);
		information.setMsgId(msgId);
		information.setClassId(classId);
		information.setMsgTitle(msgTitle);
		information.setMsgContent(msgContent);
		if (null != fileName && !"".equals(fileName)) {
			String picName = FileOP.getNewUUIDName(fileName);
			FileOP.writeFile(CodeUtil.picPath + picName, file);
			information.setMsgPic(picName);
		}
		dao.update(information);
		getFrontInfoByDb();
	}

	/**
	 * 删除资讯信息
	 * 
	 * @param ids
	 *            多个ID
	 * 
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String[] ids) {
		dao.remove(ids);
		getFrontInfoByDb();
	}

	private Map<String, List<InformationVO>> msgMap = null;
	private int size = 20;

	/**
	 * 获取前端资讯
	 * 
	 * @param size
	 *            获取某类型资讯的数量
	 * @return
	 */
	public Map<String, List<InformationVO>> getFrontInfo() {
		if (null == msgMap) {
			getFrontInfoByDb();
		}
		return msgMap;
	}

	/**
	 * 获取前端资讯
	 * 
	 * @param size
	 *            获取某类型资讯的数量
	 * @return
	 */
	public void getFrontInfoByDb() {
		Map<String, List<InformationVO>> map = new LinkedHashMap<String, List<InformationVO>>();
		String[] ids = { "A000", "B000", "C000", "D000" };
		for (String id : ids) {
			map.put(id, dao.getFrontList(id, size));
		}
		msgMap = map;
	}

	/**
	 * 随机获取特定类型知识
	 * 
	 * @param classId
	 *            顶级大类型
	 * @param size
	 *            获取资讯数量
	 * @return
	 */
	public List<InformationVO> getRandMsg(String classId, int size) {
		List<InformationVO> tmp = dao.getFrontList(classId, Integer.MAX_VALUE);
		return RandUtil.getRandList(tmp, size);
	}

}
